package com.hxzy.controller;


import com.hxzy.common.domain.AjaxResult;
import com.hxzy.common.page.TableDataInfo;
import com.hxzy.dto.HouseFeatureSearchDTO;
import com.hxzy.entity.AdminUser;
import com.hxzy.entity.HouseFeature;
import com.hxzy.framework.controller.BaseController;
import com.hxzy.service.HouseFeatureService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class HouseFeatureController extends BaseController {

    @Autowired
    private HouseFeatureService houseFeatureService;


    @GetMapping("/house/feature/list")
    public String showPage(Model model){
        return "house/feature/list";
    }

    //@RequiresPermissions(value = "house:feature:list")
    @ResponseBody
    @PostMapping("/house/feature/list")
    public TableDataInfo ajaxData(HouseFeatureSearchDTO houseFeatureSearchDTO, HttpServletRequest request){
        super.startPage(request);

        //查询
        List<HouseFeature> houseFeatureList = this.houseFeatureService.search(houseFeatureSearchDTO);

        return getDataTable(houseFeatureList);
    }


    @GetMapping("/house/feature/add")
    public String showAdd(Model model){
        return "house/feature/add";
    }

    @ResponseBody
    @PostMapping("/house/feature/add")
    public AjaxResult saveAdd(Model model, HouseFeature houseFeature){

        AdminUser adminUser = (AdminUser) SecurityUtils.getSubject().getPrincipal();
        houseFeature.setCreateTime(new Date());
        houseFeature.setUpdateTime(new Date());
        houseFeature.setCreateBy(adminUser.getLoginUserName());
        houseFeature.setUpdateBy(adminUser.getLoginUserName());

        int insert = this.houseFeatureService.insert(houseFeature);

        return toAjax(insert);
    }


    @GetMapping("/house/feature/edit/{id}")
    public String showEdit(@PathVariable("id")Integer id, Model model){

        HouseFeature houseFeature = this.houseFeatureService.selectByPrimaryKey(id);

        model.addAttribute("houseFeature", houseFeature);

        return "house/feature/edit";
    }

    @ResponseBody
    @PostMapping("/house/feature/edit")
    public AjaxResult saveEdit(Model model, HouseFeature houseFeature){

        AdminUser adminUser = (AdminUser) SecurityUtils.getSubject().getPrincipal();

        houseFeature.setUpdateTime(new Date());
        houseFeature.setUpdateBy(adminUser.getLoginUserName());

        int insert = this.houseFeatureService.updateByPrimaryKeySelective(houseFeature);

        return toAjax(insert);
    }

    @ResponseBody
    @PostMapping("/house/feature/exists")
    public Integer existsName(Integer id,String name){

     int count =  this.houseFeatureService.existsName(id,name);

     return count;
    }

}
